package com.tuanzi.loan.business.repository.credit.impl;

import static com.tuanzi.loan.generated.jooq.loan.tables.PayOrder.PAY_ORDER;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;

import org.jooq.Record;
import org.jooq.RecordMapper;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.repository.credit.custom.LoanCreditOrderStatNotEntranceCustonRepository;
import com.tuanzi.loan.core.repository.JOOQBaseRepository;
import com.tuanzi.loan.vo.request.credit.LoanCreditOrderStatNotEntranceRequest;
import com.tuanzi.loan.vo.response.credit.LoanCreditOrderStatNotEntranceResponse;
import com.tuanzi.server.loan_base.util.DateUtils;

/**
* @author chenweijun 
* @version 2018年5月6日
*/
@SuppressWarnings({ "rawtypes" })
@Repository
public class LoanCreditOrderStatNotEntranceRepositoryImpl extends JOOQBaseRepository implements LoanCreditOrderStatNotEntranceCustonRepository{
	
	@Override
	protected TableImpl getTable() {
		return PAY_ORDER;
	}

	@Override
	public Page<LoanCreditOrderStatNotEntranceResponse> list(LoanCreditOrderStatNotEntranceRequest searchRequest,
			Pageable pageable) {
		List<Object> paramList = Lists.newArrayList();
		StringBuilder  sql = new StringBuilder("SELECT a.`create_date` AS createDate,")
				.append(" a.`product_id` AS productId,")
				.append(" a.`channel_code` AS channelCode,")
			    .append(" a.front_page_uv AS frontPageUV,")
			    .append(" a.`info_page_uv` AS infoPageUV,")
			    .append(" b.full_in_information_count AS fullInInformationCount,")
			    .append(" b.create_order_count AS createOrderCount,")
			    .append(" b.pay_order_count AS payOrderCount,")
				.append(" b.order_amount AS orderAmount")
			    .append(" FROM(")
			    .append(" SELECT create_date, product_id, channel_code, `front_page_uv`, `info_page_uv`")
			    .append(" FROM stat.`loan_credit_point` GROUP BY create_date, product_id, channel_code) AS a")
			    .append(" LEFT JOIN(")
			    .append(" SELECT create_date, product_id, channel_code, full_in_information_count, create_order_count, pay_order_count, order_amount")
			    .append(" FROM stat.loan_credit_order_stat_not_entrance) AS b ON a.create_date= b.create_date")
			    .append(" AND a.product_id= b.product_id")
			    .append(" AND a.channel_code= b.channel_code")
			    .append(" WHERE 1=1");
		
		if(searchRequest.getProductId() != null) {
			sql.append(" AND a.product_id = ?");
			paramList.add(searchRequest.getProductId());
		}
		
		if(searchRequest.getChannelCode() != null) {
			sql.append(" AND a.channel_code = ?");
			paramList.add(searchRequest.getChannelCode());
		}
		
		if(searchRequest.getStartDate() != null) {
			sql.append(" AND a.create_date >= ?");
			paramList.add(DateUtils.formatDateToYyyyMMdd(searchRequest.getStartDate()));
		}else{
			sql.append(" AND a.create_date >= ?");
			paramList.add(LocalDate.now().toString());
		}
		
		if(searchRequest.getEndDate() != null) {
			sql.append(" AND a.create_date <=?");
			paramList.add(DateUtils.formatDateToYyyyMMdd(searchRequest.getEndDate()));
		}
		
		sql.append(" ORDER BY a.create_date DESC");
		
		Long count = dslContext.fetchOne("SELECT COUNT(*) FROM (" + sql + ") AS i", paramList.toArray()).into(Long.class);
		
		paramList.add(pageable.getOffset());
		paramList.add(pageable.getPageSize());
		
		return new PageImpl<LoanCreditOrderStatNotEntranceResponse>(dslContext.fetch(sql.toString() + " LIMIT ?,? ", paramList.toArray()).map(new RecordMapper<Record, LoanCreditOrderStatNotEntranceResponse>() {

			@Override
			public LoanCreditOrderStatNotEntranceResponse map(Record record) {
				
				Date createDate = record.get("createDate", Date.class);
				Integer productId = record.get("productId", Integer.class);
				Integer channelCode = record.get("channelCode", Integer.class);
				Integer frontPageUV = record.get("frontPageUV", Integer.class);
				Integer infoPageUV = record.get("infoPageUV", Integer.class);
				Integer fullInInformationCount = record.get("fullInInformationCount", Integer.class);
				Integer createOrderCount = record.get("createOrderCount", Integer.class);
				Integer payOrderCount = record.get("payOrderCount", Integer.class);
				Float orderAmount = record.get("orderAmount", Float.class);
				
				LoanCreditOrderStatNotEntranceResponse response = new LoanCreditOrderStatNotEntranceResponse();
				response.setCreateDate(createDate);
				response.setProductId(productId);
				response.setChannelCode(channelCode);
				response.setFrontPageUV(frontPageUV != null ? frontPageUV : 0);
				response.setInfoPageUV(infoPageUV != null ? infoPageUV : 0);
				response.setFullInInformationCount(fullInInformationCount != null ? fullInInformationCount : 0);
				response.setCreateOrderCount(createOrderCount != null ? createOrderCount : 0);
				response.setPayOrderCount(payOrderCount != null ? payOrderCount : 0);
				response.setOrderAmount(orderAmount != null ? orderAmount : 0.00F);
				
				return response;
			}
		}), pageable, count);

	}
	

}
